/**
 * 
 */
package com.telenav.techtalk;

import java.io.InputStream;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;

/**
 * @author yxyao
 *
 */
public class ColorMatrixActivity extends Activity{

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new ColorMatrixView(this), new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
    }
    
    class ColorMatrixView extends View {
        
        private Bitmap icon;
        
        public ColorMatrixView(Context context){
            super(context);
            icon = BitmapFactory.decodeResource(getResources(), R.drawable.telenav_icon);
        }
        
        protected void onDraw(Canvas canvas) {
            canvas.drawBitmap(icon, 10, 10, null);
            ColorMatrix cm = new ColorMatrix();
            setContrast(cm, 0.5f);
            Paint p = new Paint();
            p.setColorFilter(new ColorMatrixColorFilter(cm));
            canvas.drawBitmap(icon, 30+icon.getWidth(), 10, p);
            setGrey(cm);
            p.setColorFilter(new ColorMatrixColorFilter(cm));
            canvas.drawBitmap(icon, 50+icon.getWidth()*2,10, p);
        }
        
     
    }
    
    private static void setGrey(ColorMatrix cm) {
        final float avgFactor = 1.0f/3.0f;
        cm.set(new float[]{
                avgFactor,avgFactor,avgFactor,0,0,
                avgFactor,avgFactor,avgFactor,0,0,
                avgFactor,avgFactor,avgFactor,0,0,
                0, 0, 0, 1,0
        });
    }
    
    private static void setContrast(ColorMatrix cm, float contrast) {
        float scale = contrast + 1.f;
           float translate = (-.5f * scale + .5f) * 255.f;
        cm.set(new float[] {
               scale, 0, 0, 0, translate,
               0, scale, 0, 0, translate,
               0, 0, scale, 0, translate,
               0, 0, 0, 1,0 });
    }
}
